﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using Ninject;
using sdm.exam.domain.Abstract;
using sdm.exam.domain.Entities;

namespace sdm.exam.domain.DataContext
{
    public class EducationDomain
    {
        private readonly ISdmExamRepository _repository = null;
        public EducationDomain()
        {
            _repository = Ninjector.Kernel.Get<ISdmExamRepository>();
            Ninjector.Kernel.Bind<ISdmExamRepository>().ToConstant(new DataContext(ConnectionStrings.StandardConnection));
        }

        /// <summary>
        /// Gets a complete list of educations.
        /// </summary>
        /// <returns>The list of educations.</returns>
        public List<Entities.Education> GetListOfEducations()
        {
            List<Education> list = null;
            try
            {
                var q = from item in _repository.Educations select item;
                list = q.ToList();
            }
            catch(Exception e)
            {
                throw new Exception("Something went wrong!");
            }
            return list;
        }

        /// <summary>
        /// Gets a specific education.
        /// </summary>
        /// <param name="id">The id </param>
        /// <returns></returns>
        public Education GetEducationById(int id)
        {
            Education edu = _repository.Educations.FirstOrDefault(m => m.Id == id);
            return edu;
        }

        /// <summary>
        /// Adds an education to the database.
        /// </summary>
        /// <returns>true if succeed otherwise false</returns>
        public bool AddEducation(string name)
        {
            try
            {
                var education = new Education {Name = name};
                _repository.Educations.Add(education);
                _repository.SaveChanges();

                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool AddEducation(Education education)
        {
            try
            {
                _repository.Educations.Add(education);
                _repository.SaveChanges();

                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
    }
}
